package com.herench.bs.control;


import com.herench.bs.agent.SatisfactionInHosAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/clinic")
public class SatisfactionInHosController {

    @Autowired
    private SatisfactionInHosAgent satisfactionInHosAgent;


    @RequestMapping("/outpatient")
    public @ResponseBody
    Map<String, Object> outpatient(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> res = new HashMap<>();
        String[] questionIds = request.getParameterValues("questionId");
        String phone = request.getParameter("hidUsermobile");
//        String phone =  "13752361968";
        Map<String, Object> params = new HashMap<>();
        params.put("username", phone);
//        Map<String,Object> user = userDao.findOne(params);
        List<Map<String, Object>> records = new ArrayList<>();

        if (questionIds != null && questionIds.length > 0) {
            for (String questionId : questionIds) {
                InHos record = new InHos();
                record.setQuestionId(questionId);
                String answerCode = request.getParameter(questionId + "_answer");
                String cause = request.getParameter(questionId + "_cause");
                record.setAnswerCode(answerCode);
                record.setCause(cause);

                Map<String, Object> el = new HashMap<>();
                el.put("phone", phone);
                el.put("questionId", questionId);
                el.put("answer", answerCode);
                el.put("cause", cause);
//                el.put("idCard", user.get("personId"));
//                el.put("name", user.get("name"));

//                satisfactionOutpatientDao.add(el);
                records.add(el);
            }
        }
        satisfactionInHosAgent.getOutpatient(records);
//        res.put("result",true);
        res.put("success", "success");
        return res;
    }

    @RequestMapping("/inHos")
    public @ResponseBody
    Map<String, Object> inHos(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> res = new HashMap<>();
        String[] questionIds = request.getParameterValues("problem");
        String deptName = request.getParameter("deptName");
        String name = request.getParameter("patientName");
        String phone = request.getParameter("patientPhone");
        String user = request.getParameter("hidUsermobile");
//        String user = "13752361968";
        Map<String, Object> params = new HashMap<>();


        params.put("username", user);
//        Map<String,Object> resDao = userDao.findOne(params);
        List<Map<String, Object>> inHos = new ArrayList<>();
        if (questionIds != null && questionIds.length > 0) {
            for (String questionId : questionIds) {
                InHos inHos1 = new InHos();
                inHos1.setQuestionId(questionId);
                String answerCode = request.getParameter("satisfaction" + questionId);
                String cause = request.getParameter("reason" + questionId);
                inHos1.setAnswerCode(answerCode);
                inHos1.setCause(cause);
                Map<String, Object> el = new HashMap<>();
                el.put("questionId", questionId);
                el.put("answer", answerCode);
                el.put("cause", cause);
                el.put("deptName", deptName);
//          el.put("idCard",resDao.get("personId"));
                el.put("name", name);
                el.put("phone", phone);
//          satisfactionInHosDao.add(el);

                inHos.add(el);
            }
        }
        String code = satisfactionInHosAgent.getInHos(inHos);
        if("0".equals(code) || "".equals(code)){
            res.put("result", true);
        }else {
            res.put("result", false);
        }

        return res;
    }

    class InHos {
        private String questionId;
        private String answerCode;
        private String cause;


        public String getQuestionId() {
            return questionId;
        }

        public void setQuestionId(String questionId) {
            this.questionId = questionId;
        }

        public String getAnswerCode() {
            return answerCode;
        }

        public void setAnswerCode(String answerCode) {
            this.answerCode = answerCode;
        }

        public String getCause() {
            return cause;
        }

        public void setCause(String cause) {
            this.cause = cause;
        }
    }
}
